package com.example.todolist.view.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.example.todolist.databinding.FragmentAddCategoryBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class AddItemDialogFragment(
    private val onItemAdded: (String) -> Unit,
    private val onAddBtn: () -> Unit
) : BottomSheetDialogFragment() {
    private lateinit var binding: FragmentAddCategoryBinding
//    private var listener: OnCategoryAddedListener? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentAddCategoryBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val etTitle = binding.etCategory
        val btnCancel = binding.btnCancel
        val btnSave = binding.btnSave
        // 取消按钮
        btnCancel.setOnClickListener {
            dismiss()
//            onAddBtn()
        }

        // 保存按钮
        btnSave.setOnClickListener {
            val title = etTitle.text.toString().trim()
            if (title.isNotEmpty()) {
                onItemAdded(title)
//                onAddBtn()
                dismiss()
            } else {
                Toast.makeText(requireContext(), "请输入标题", Toast.LENGTH_SHORT).show()
            }
        }
    }

    //
//    // 设置回调接口
//    fun setListener(listener: OnCategoryAddedListener) {
//        this.listener = listener
//    }
//
//    // 回调接口
//    interface OnCategoryAddedListener {
//        fun onItemAdded(title: String)
//    }
    override fun onDestroy() {
        super.onDestroy()
        onAddBtn()
    }
}